package Control;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import GUI.TestingStage_GUI;
import Entity.MsgPackage;
import GUI.ScreenManager;
import Entity.FailureReport;

public class TestingStageControl {
	
	private TestingStage_GUI test_stage_gui;
	private MsgPackage testMsg;
	private FailureReport fail_rep; 
	
	public TestingStageControl(){
		test_stage_gui= new  TestingStage_GUI();
		testMsg= new MsgPackage();
		fail_rep= new FailureReport(); 
	}

	public TestingStage_GUI getTextingGui() {
		return test_stage_gui;
	}
	
	public boolean testing_Submit_pressed(Integer reqID) {
		String x,y;
        x =(String) test_stage_gui.getDecision().getSelectedItem();
		y= test_stage_gui.getComments().getText();
		if(x.equals(""))
		{
			JOptionPane.showMessageDialog(null, "You have to enter your decision", "ERROR", JOptionPane.ERROR_MESSAGE);
	        return false;
		}
		if(x.equals("failure") && y.equals("")){
			JOptionPane.showMessageDialog(null, "Not have to add comments", "ERROR", JOptionPane.ERROR_MESSAGE);
	        return false;
		}
		fail_rep.setReqID(reqID);
		fail_rep.setDecision(x);
		fail_rep.setComments(y);
		if(x.equals("failure")){
			testMsg.setOp_code(OP_CODE.SET_FAILURE_REPORT);
			testMsg.setObject(fail_rep);
		}
		else{
			testMsg.setObject(reqID);
			testMsg.setOp_code(OP_CODE.SET_TESTING_STAGE);
		}
        ScreenManager.getConnection().accept(testMsg);
		
        while(ScreenManager.getConnection().getAnswerReceivedFlag() == false);	//wait until flag is changed and shows that result was received from server
    	
		Object o = ScreenManager.getConnection().getResultobject();
        JOptionPane.showMessageDialog(null, "The report send successfully", "", JOptionPane.INFORMATION_MESSAGE);

		return true;
	}
}